package com.jieao.template.common.util;

import com.jieao.template.entity.accountMgr.AccountDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.LogoutAware;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.Objects;

/**
 * @author Jzw
 * @className CurrentloginUtil
 * @since 2020-04-28 09:50
 */
public class CurrentlyLoggedInUtil
{

    @Autowired
    private static RedisSessionDAO redisSessionDAO;

    /**
     * 获取当前登录用户的信息
     *
     * @return com.inspectioncommission.portal.pojo.admin.login.AdminAccountDO
     * @author 姜兆伟
     * @date 2019/9/21 14:46
     */
    public static AccountDO getUserInfo()
    {
        Object principal = Objects.requireNonNull(SecurityUtils.getSubject().getPrincipal());
        return (AccountDO) principal;
    }

    /**
     * 获取当前登录用户的账户id
     *
     * @return com.inspectioncommission.portal.pojo.admin.login.AdminAccountDO
     * @author 姜兆伟
     * @date 2019/9/21 14:46
     */
    public static String getUserId()
    {
        return getUserInfo().getId();
    }

    /**
     * 获取当前登录用户的账户 账号
     *
     * @param
     * @return java.lang.String
     * @author Jzw
     * @since 2020/7/15 14:33
     */
    public static String getAccount()
    {
        return getUserInfo().getAccount();
    }

    /**
     * Delete user cache information
     *
     * @param username        username
     * @param isRemoveSession delete or not session( sign out )
     * @return void
     * @author Jzw
     * @since 2020/5/7 10:19
     */
    public static void deleteCache(String username, boolean isRemoveSession)
    {
        //从缓存中获取Session
        Session session = null;
        Collection<Session> sessions = redisSessionDAO.getActiveSessions();
        AccountDO sysUserEntity;
        Object attribute = null;
        for (Session sessionInfo : sessions)
        {
            //遍历Session,找到该用户名称对应的Session
            attribute = sessionInfo.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            if (attribute == null)
            {
                continue;
            }
            sysUserEntity = (AccountDO) ((SimplePrincipalCollection) attribute).getPrimaryPrincipal();
            if (sysUserEntity == null)
            {
                continue;
            }
            if (Objects.equals(sysUserEntity.getAccount(), username))
            {
                session = sessionInfo;
            }
        }
        if (session == null || attribute == null)
        {
            return;
        }
        //删除session
        if (isRemoveSession)
        {
            redisSessionDAO.delete(session);
        }
        //删除Cache，在访问受限接口时会重新授权
        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        Authenticator authc = securityManager.getAuthenticator();
        ((LogoutAware) authc).onLogout((SimplePrincipalCollection) attribute);
    }
}

